
package arffreader;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * 
 */
class HeaderInserter {

    public void addAttributte(String attr, String tipo, String filestr) throws Exception {
        String linea;
        boolean data = false;
        String attributeWrite = "@attribute " + attr + " " + tipo;
        try {
            FileReader fr = new FileReader(filestr);
            FileWriter fw = new FileWriter("temp");

            BufferedReader br = new BufferedReader(fr);
            BufferedWriter bw = new BufferedWriter(fw);

            while ((linea = br.readLine()) != null) {
                if (linea.contains("@data")||linea.contains("@DATA")){
                    data = true;
                    break;
                }
                bw.write(linea, 0, linea.length());
                bw.write("\n");
            }
            bw.write(attributeWrite, 0, attributeWrite.length());

            if (data) {
                bw.write("\n");
                bw.write(linea, 0, linea.length());
                while ((linea = br.readLine()) != null) {
                    bw.write("\n");
                    bw.write(linea, 0, linea.length());
                }
            }
            bw.write("\n");
            br.close();
            bw.close();
            fr.close();
            fw.close();
            File temp = new File("temp");

            temp.renameTo(new File(filestr));
            
            File temp2 = new File("temp");
            temp2.delete();
            

        } catch (Exception ex) {
            throw ex;
        }
    }
}
